package com.wwdx.controller.admin;

import com.google.common.collect.Lists;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Set;

/**
 * @author liguoliang
 * @date 2018-1-28
 */
@Controller
@RequestMapping("/system/wwdx_book_online")
public class WwdxBookOnlineController extends BaseController {

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        ModelAndView mav = new ModelAndView();
        Page redisPages = super.getRedisPages(page, rows);
        Set<Object> rangeByScore = zSetOperations.range(ConstantHolder.BOOK_ONLINE_ZSET, redisPages.getPage(), redisPages.getRows());
        List<WwdxBookModel> wwdxBookModels = Lists.newArrayListWithExpectedSize(rangeByScore.size());
        for (Object bookid:rangeByScore){
            wwdxBookModels.add(wwdxBookService.getByBookId(Long.valueOf(bookid.toString())));
        }
        mav.addObject("listSize", zSetOperations.size(ConstantHolder.BOOK_ONLINE_ZSET));
        mav.addObject("list", wwdxBookModels);
        return mav;
    }
}